<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
			String user_name = (session
			.getValue("SPRING_SECURITY_LAST_USERNAME")).toString();
%>
<html>
	<head>
		<title>电流曲线</title>
		<link id=style href="../styles/theme/blue/frame.css" type="text/css"
			rel="stylesheet" />
		<style type="text/css">
.divLinechart {
	width: 100%;
	margin: 5px;
}
</style>
		<script type='text/javascript' src='../ajax-pushlet-client.js'></script>
		<script type='text/javascript' src='../js/jquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery.blockUI.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highcharts.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highChartsTheme.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/scripts/curveCommon.js"></script>
		<script>
function onData(event){

	var currentData = event.get("currentData");
	// 处理A相电流曲线返回数据
    if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101080b") {
    	$("#aCurrentData").val(currentData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101100b") {// 处理B相电流曲线返回数据
    	$("#bCurrentData").val(currentData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101200b") {// 处理C相电流曲线返回数据
    	$("#cCurrentData").val(currentData);
    }
    
    // 如果响应都返回后，根据数据绘制曲线图
    if ($("#aCurrentData").val() != "" && $("#bCurrentData").val() != "" && $("#cCurrentData").val() != "" && $("#aCurrentData").val() != undefined && $("#bCurrentData").val() != undefined && $("#cCurrentData").val() != undefined ) {
	    // 准备A、B、C相电流
	    var aCurrentDataArr = $("#aCurrentData").val().split(":");
	    var bCurrentDataArr = $("#bCurrentData").val().split(":");
	    var cCurrentDataArr = $("#cCurrentData").val().split(":");
	    var startTime = event.get("startTime");
		
		var aCurrentDataArrDouble = new Array();
		var bCurrentDataArrDouble = new Array();
		var cCurrentDataArrDouble = new Array();
		for (var i=0; i<aCurrentDataArr.length; i++) {
			aCurrentDataArrDouble[i] = parseFloat(aCurrentDataArr[i]);
			bCurrentDataArrDouble[i] = parseFloat(bCurrentDataArr[i]);
			cCurrentDataArrDouble[i] = parseFloat(cCurrentDataArr[i]);
		}
		
		// 修正各曲线的最大值
		var aCorrectYAxisMax = correctYAxisMax(aCurrentDataArrDouble, 2/3);
		var bCorrectYAxisMax = correctYAxisMax(bCurrentDataArrDouble, 2/3);
		var cCorrectYAxisMax = correctYAxisMax(cCurrentDataArrDouble, 2/3);
		
		var maxVal = -999999999;
		maxVal = aCorrectYAxisMax > maxVal ? aCorrectYAxisMax : maxVal;
		maxVal = bCorrectYAxisMax > maxVal ? bCorrectYAxisMax : maxVal;
		maxVal = cCorrectYAxisMax > maxVal ? cCorrectYAxisMax : maxVal;
		maxVal = maxVal > 0.0 ? maxVal : 10;
		
		// 修正各曲线的最小值
		var aCorrectYAxisMin = correctYAxisMin(aCurrentDataArrDouble, 2/3);
		var bCorrectYAxisMin = correctYAxisMin(bCurrentDataArrDouble, 2/3);
		var cCorrectYAxisMin = correctYAxisMin(cCurrentDataArrDouble, 2/3);
		
		var minVal = 0;
		minVal = aCorrectYAxisMin < minVal ? aCorrectYAxisMin : minVal;
		minVal = bCorrectYAxisMin < minVal ? bCorrectYAxisMin : minVal;
		minVal = cCorrectYAxisMin < minVal ? cCorrectYAxisMin : minVal;
		
		// 准备时间
		var time = new Array();
		time[0] = startTime;
		var date= new Date(Date.parse(startTime.replace(/-/g,   "/"))); 
		for (var i=1; i<aCurrentDataArr.length; i++) {
		
			var date2Minsec = date.getTime();
		
			var addFifteen = 1000 * 60 * 15;
			date2Minsec += addFifteen;
			date = new Date(date2Minsec);
			time[i]= date2Str(date);
		}
	
		// 创建折线图，生成后放置在id为container的div中
		titleText = "电流曲线";
		colors = threePhaseColor;
		yAxisText = "电流(A)";
		unit = "A";
		seriesVal = [{
			name: 'A相电流',
			data: aCurrentDataArrDouble
		}, {
			name: 'B相电流',
			data: bCurrentDataArrDouble
		},{
			name: 'C相电流',
			data: cCurrentDataArrDouble
		}];
		var container = parent.window.frames["lineChart"].document.getElementById("container");
		
		var chart = showChart(container, titleText, startTime, colors, time, yAxisText, maxVal, minVal, unit, seriesVal);
    }
}

// pushlet ajax开始，监听/source/current的报文
PL._init('<%=user_name%>');
PL.joinListen('/source/current');
</script>
	</head>
	<body style="overflow-y: hidden; overflow-x: hidden">
		<input type="hidden" name="aCurrentData" id="aCurrentData" />
		<input type="hidden" name="bCurrentData" id="bCurrentData" />
		<input type="hidden" name="cCurrentData" id="cCurrentData" />
		<div class="divLinechart" id="container">
		</div>
	</body>
</html>
